package com.kingdee.pda.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.basecommon.utils.PreferencesUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.ui.widget.SelectorTextView;
import com.kingdee.pda.util.ConstantPreference;
import com.kingdee.pda.util.LanguageUtil;

/**
 * Created by zhanlg
 * DATE 2019/4/28
 * TIME 19:17
 */

public class LanguageSettingActivity extends BaseActivity implements View.OnClickListener {


    private ListView mlvLanguage;
    private LanguageListAdapter mAdapter;


    public static void actionStartForResult(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, LanguageSettingActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_language_setting);
    }

    @Override
    protected void initView() {
        mlvLanguage = ((ListView) findViewById(R.id.lv_language));
        findViewById(R.id.iv_back).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mAdapter = new LanguageListAdapter(this);
        mlvLanguage.setAdapter(mAdapter);
        setLanguageItemSelectListener();
    }

    private void setLocale() {
        if (mAdapter != null) {
            if (!LanguageUtil.getCurrentLocaleText().equals(mAdapter.getSelectedLocale())) {
//                PreferencesUtils.putLong(MyApplication.getInstance().getApplicationContext(), ConstantPreference.SPF_KEY_SYNC_TABLE_FLAG, 0);//需重新同步数据
                PreferencesUtils.putString(LanguageSettingActivity.this, ConstantPreference.SPF_KEY_SELECTED_LOCALE, mAdapter.getSelectedLocale());
                setResult(RESULT_OK);
            }
        }
        LanguageSettingActivity.this.finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                setLocale();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setLocale();
        super.onBackPressed();
    }

    public void setLanguageItemSelectListener() {
        mlvLanguage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mAdapter.setSelectedPosition(position);
            }
        });
    }

    private class LanguageListAdapter extends BaseAdapter {

        private Context mContext;
        private String[] languages;
        private int selectedPosition = 0;

        public LanguageListAdapter(Context context) {
            this.mContext = context;
            languages = getResources().getStringArray(R.array.language_list);
            selectedPosition = getCurrentPosition();
        }

        private int getCurrentPosition() {
            for (int i = 0; i < languages.length; i++) {
                if (LanguageUtil.getCurrentLocaleText().equals(languages[i])) {
                    return i;
                }
            }
            return 0;
        }

        @Override
        public int getCount() {
            return languages.length;
        }

        @Override
        public Object getItem(int position) {
            return languages[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_common_list_select, null);
                holder = new ViewHolder();
                holder.tv_language = ((SelectorTextView) convertView.findViewById(R.id.tv_name));
                holder.iv_selected = ((ImageView) convertView.findViewById(R.id.iv_selected));

                convertView.setTag(holder);
            } else {
                holder = ((ViewHolder) convertView.getTag());
            }
            holder.tv_language.setText(languages[position]);
            holder.tv_language.setSelected(selectedPosition == position);
            holder.iv_selected.setVisibility(selectedPosition == position ? View.VISIBLE : View.INVISIBLE);

            return convertView;
        }

        class ViewHolder {
            SelectorTextView tv_language;
            ImageView iv_selected;
        }

        public void setSelectedPosition(int position) {
            this.selectedPosition = position;
            notifyDataSetChanged();
        }

        public String getSelectedLocale() {
            return languages[selectedPosition];
        }
    }


    @Override
    protected void onNetworkAvailable() {

    }

    @Override
    protected void onNetworkError() {

    }
}
